package com.sservice.service.service.impl;

import com.sservice.common.Constants;
import com.sservice.model.dao.IGoodsDao;
import com.sservice.model.dao.PurchaseOrderBillMapper;
import com.sservice.model.entity.Goods;
import com.sservice.model.entity.Purchase;
import com.sservice.model.entity.PurchaseOrder;
import com.sservice.model.entity.PurchaseOrderBill;
import com.sservice.model.vo.PurchaseOrderBillVo;
import com.sservice.service.service.PurchaseOrderBillService;
import com.sservice.service.service.PurchaseOrderService;
import com.sservice.service.service.PurchaseService;
import com.sservice.web.common.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseOrderBillServiceImpl implements PurchaseOrderBillService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PurchaseOrderBillMapper dataDao;
    @Autowired
    private PurchaseOrderService orderService;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private IGoodsDao goodsDao;
    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dataDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PurchaseOrderBill record) {
        return dataDao.insert(record);
    }

    @Override
    public int insertSelective(PurchaseOrderBill record) {
        return dataDao.insertSelective(record);
    }

    private int getMaxInNoOrder() {
        String str = dataDao.queryCurrentDayMaxNo();
        if (str == null) {
            return 0;
        } else {
            String number = str.split("-")[1];
            return Integer.valueOf(number);
        }
    }

    @Transactional
    @Override
    public synchronized Object createBill(PurchaseOrderBill record) {
        PurchaseOrder order = orderService.selectByPrimaryKey(Integer.valueOf(record.getOrderId()));

        PurchaseOrder order_ = new PurchaseOrder();
        order_.setNumberReceiving(record.getNumber().add(order.getNumberReceiving()));// 收货中数量
        BigDecimal canReceive = order.getNumber().subtract(order.getNumberReceiving());
        canReceive = canReceive.subtract(order.getNumberActualReceive());//
        if (record.getNumber().compareTo(canReceive) == 1) {
            return "发货数量不能大于采购订单可发货数量";
        }
        record.setTaxRatio(order.getTaxRatio());
        order_.setId(order.getId());
        orderService.updateByPrimaryKeySelective(order_); // 订单数据更新
        record.setClienteleId(Integer.valueOf(order.getClienteleId()));
        record.setClienteleName(order.getClienteleName());
        record.setUnitCode(order.getUnitCode());
        record.setCheckStatus("0");
        record.setPrice(order.getCurrentPrice());
        record.setGoodsId(order.getGoodsId());
        record.setQuality(order.getQuality());
        Goods g = goodsDao.selectByPrimaryKey(Integer.valueOf(order.getGoodsId()));
        record.setGoodsNo(g.getCode());
        record.setGoodsName(g.getName());
        record.setNo(Constants.generateServiceNO("S", Constants.generateLimitNumberStr(getMaxInNoOrder() + 1, 3)));
//        record.set
        return dataDao.insertSelective(record);
    }

    /**
     * @param record
     * @return
     */
    @Transactional
    @Override
    public synchronized Object createBillByCombineOrder(PurchaseOrderBillVo record) {
//        List<OrderPurchaseOrder> opoList = new ArrayList<OrderPurchaseOrder>();
        String[] orderIds = record.getOrderIds().split(",");
        String[] nums = record.getNumbers().split(",");
        String[] batchs = record.getBatchNum().split(",");
        record = createPurchaseOrderBill(record);
        for (int i = 0; i < orderIds.length; i++) {
            record.setId(null);
            record.setNumber(BigDecimal.valueOf(Double.valueOf(nums[i])));
            record.setOrderId(orderIds[i]);
            record.setBatchNum(batchs[i]);
            PurchaseOrder order = orderService.selectByPrimaryKey(Integer.valueOf(orderIds[i]));
            // 批量插入
            BigDecimal canReceive = order.getNumber().subtract(order.getNumberReceiving()).subtract(order.getNumberActualReceive());
            if (record.getNumber().compareTo(canReceive) == 1) {
                throw new RuntimeException("发货数量不能大于采购订单可发货数量");
            }
            orderService.changeNumber(
                    order.getId(),
                    record.getNumber(),
                    BigDecimal.ZERO
            );
            record.setPrice(order.getCurrentPrice());
            dataDao.insertSelective(record);
        }
        return record.getId();
    }


    /***
     * 创建 采购 bill
     *
     * @param record
     * @return
     */
    public PurchaseOrderBillVo createPurchaseOrderBill(PurchaseOrderBillVo record) {
        String[] orderIds = record.getOrderIds().split(",");
        String[] nums = record.getNumbers().split(",");
        PurchaseOrder order1 = orderService.selectByPrimaryKey(Integer.valueOf(orderIds[0]));
        record.setTaxRatio(order1.getTaxRatio());
        record.setClienteleId(Integer.valueOf(order1.getClienteleId()));
        record.setClienteleName(order1.getClienteleName());
        record.setUnitCode(order1.getUnitCode());
        record.setCheckStatus("0");
        record.setPrice(order1.getCurrentPrice());
        record.setGoodsId(order1.getGoodsId());
        record.setQuality(order1.getQuality());
        record.setNumber(ArrayUtils.strArrayTotal(nums));
        //
        Goods g = goodsDao.selectByPrimaryKey(Integer.valueOf(order1.getGoodsId()));
        record.setGoodsName(g.getName());
        record.setNo(Constants.generateServiceNO("S", Constants.generateLimitNumberStr(getMaxInNoOrder() + 1, 3)));
        return record;
    }

    /***
     *  多个采购订单发货
     * @param
     * @return
     */
//    @Transactional
//    @Override
//    public synchronized Object multiOrderToBill(PurchaseOrderBill record) {
//        PurchaseOrder order = orderService.selectByPrimaryKey(Integer.valueOf(record.getOrderId()));
//
//        PurchaseOrder order_ = new PurchaseOrder();
//        order_.setNumberReceiving(record.getNumber().add(order.getNumberReceiving()));// 收货中数量
//        BigDecimal canReceive = order.getNumber().subtract(order.getNumberReceiving());
//        canReceive = canReceive.subtract(order.getNumberActualReceive());//
//        if (record.getNumber().compareTo(canReceive) == 1) {
//            return "发货数量不能大于采购订单可发货数量";
//        }
//        record.setTaxRatio(order.getTaxRatio());
//        order_.setId(order.getId());
//        orderService.updateByPrimaryKeySelective(order_); // 订单数据更新
//        record.setClienteleId(Integer.valueOf(order.getClienteleId()));
//        record.setClienteleName(order.getClienteleName());
//        record.setUnitCode(order.getUnitCode());
//        record.setCheckStatus("0");
//        record.setPrice(order.getCurrentPrice());
//        record.setGoodsId(order.getGoodsId());
//        record.setQuality(order.getQuality());
//        Goods g = goodsDao.selectByPrimaryKey(Integer.valueOf(order.getGoodsId()));
//        record.setGoodsNo(g.getCode());
//        record.setGoodsName(g.getName());
//        record.setNo(Constants.generateServiceNO("S", Constants.generateLimitNumberStr(getMaxInNoOrder() + 1, 3)));
////        record.set
//        return dataDao.insertSelective(record);
//    }
    @Override
    public PurchaseOrderBill selectByPrimaryKey(Integer id) {
        return dataDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PurchaseOrderBill record) {
        return dataDao.updateByPrimaryKeySelective(record);
    }


    /**
     * 入库
     *
     * @param record
     * @return
     */
    @Transactional
    @Override
    public synchronized Object instore(PurchaseOrderBill record) {
        PurchaseOrderBill pob = dataDao.selectByPrimaryKey(record.getId());
        if (!pob.getCheckStatus().equals("1")) {
            return "收货单未审核或未审核通过, 不能入库!";
        }
        PurchaseOrder po = orderService.selectByPrimaryKey(Integer.valueOf(pob.getOrderId()));
        BigDecimal compare_ = pob.getNumber().subtract(pob.getNumberReceive());
        int compare = compare_.compareTo(record.getNumberReceive());
        if ("2".equals(pob.getType())) {
            if (pob.getNumberSended().compareTo(record.getNumberReceive()) != 0) {
                return "实收数量和实提数量不一致，不能操作!";
            }
        }
        if (compare != -1) {
            PurchaseOrder po1 = new PurchaseOrder();
            po1.setId(po.getId());
            po1.setNumberReceiving(po.getNumberReceiving().subtract(pob.getNumber()));
            if (po1.getNumberReceiving().compareTo(BigDecimal.valueOf(0.0)) == -1) {
                po1.setNumberReceiving(BigDecimal.valueOf(0.0)); ///处理一个车号类型批号被分配到多个销售订单录入实提数量，多次“入库”车号类型收货单
            }
            po1.setNumberActualReceive(po.getNumberActualReceive().add(record.getNumberReceive()));
            if (po.getNumber().compareTo(po1.getNumberActualReceive()) == 0) {
                po1.setStatus("4"); // 执行完毕
            }
            orderService.updateByPrimaryKeySelective(po1); //采购订单数据更新
            Purchase p = purchaseService.selectByPrimaryKey(Integer.valueOf(po.getPurchaseId()));
            Purchase p1 = new Purchase();
            p1.setId(Integer.valueOf(po.getPurchaseId()));
            p1.setNumberCharging(p.getNumberCharging().subtract(record.getNumberReceive())); // 从采购单中交易中数量扣减已经收货数量
            p1.setNumberCharged(p.getNumberCharged().add(record.getNumberReceive())); // 从采购单中已交易数量增加已经收货数量
            purchaseService.updateByPrimaryKeySelective(p1);
            record.setStatus("2");
            if ("0".equals(pob.getType())) { // 车号1次入库
                record.setNumberSending(BigDecimal.ZERO);
            }
            return dataDao.updateByPrimaryKeySelective(record);
        } else {
            return "参数非法,禁止提交!";
        }
    }

    /***
     * 数组字段转化为key-value map
     * @param record
     * @return
     */
    public Map<String, BigDecimal> generateMap(PurchaseOrderBillVo record) {
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        String[] ids = record.getOrderIds().split(",");
        String[] nums = record.getNumbers().split(","); //  // 获取实际入库数量
        for (int i = 0; i < ids.length; i++) {
            map.put(ids[i], BigDecimal.valueOf(Double.valueOf(nums[i])));
        }
        return map;
    }

    /**
     * 申请付款
     *
     * @param record
     * @return
     */
    @Transactional
    @Override
    public Object applyPaper(PurchaseOrderBillVo record) {
        List<PurchaseOrderBillVo> list = dataDao.queryByPage(record);
        int id = 0;
        for (PurchaseOrderBillVo pobv : list) {
            PurchaseOrderBill po = new PurchaseOrderBill();
            PurchaseOrder po1 = purchaseOrderService.selectByPrimaryKey(Integer.valueOf(pobv.getOrderId()));
            if (id == 0) id = po1.getId();
            if (pobv.getId() < id) id = po1.getId();
            po.setId(pobv.getId()); //
            if (!("0".equals(po.getPaperStatus()) || po.getPaperStatus() == null)) {
                throw new RuntimeException("收货单已经申请发票，不能再次申请！");
            }
            po.setPaperStatus("1");
            po.setPaperApplier(record.getPaperApplier()); //
            po.setTaxRatio(po1.getTaxRatio());
            po.setPaperApplyTime(new Date());
            dataDao.updateByPrimaryKeySelective(po);
        }
        return id;
    }

    /***
     * 合并入库
     * @param record
     * @return
     */
    @Transactional
    @Override
    public synchronized Object instoreCombine(PurchaseOrderBillVo record) {

        String[] nums = record.getNumbers().split(",");
        Map<String, BigDecimal> map = generateMap(record);
        List<PurchaseOrderBillVo> list = dataDao.queryByPage(record);
        for (PurchaseOrderBillVo pob : list) {
            instoreBySingle(pob, map, nums);
        }
        return null;
    }

    @Transactional
    public Object instoreBySingle(PurchaseOrderBillVo pob, Map<String, BigDecimal> map, String[] nums) {
        if (!pob.getCheckStatus().equals("1")) {
            return "收货单未审核或未审核通过, 不能入库!";
        }
        PurchaseOrder po = orderService.selectByPrimaryKey(Integer.valueOf(pob.getOrderId()));
        BigDecimal actualReceive = map.get(pob.getId() + "");// 本次操作实际入库数量
        if ("2".equals(pob.getType())) {
            if (pob.getNumberSended().compareTo(actualReceive) != 0) {
                throw new RuntimeException("介绍信类型收货单实收数量和实提数量不一致，不能操作!");
            }
        }
        BigDecimal compare_ = pob.getNumber().subtract(pob.getNumberReceive()); // 收货未入库数量
        int compare = compare_.compareTo(actualReceive); // 本次入库数量 和 实际未入库数量比较
        if (compare != -1) {
            orderService.changeNumber(po.getId(),
                    pob.getNumber().negate(),
                    actualReceive); //采购订单数据更新
            purchaseService.changeNumber(
                    Integer.valueOf(po.getPurchaseId()),
                    BigDecimal.ZERO, //采购数量不变
                    actualReceive, // 已交易数量增加
                    actualReceive.negate() // 交易中数量扣减
            );
            changeNumber(pob.getId(), actualReceive, BigDecimal.ZERO, BigDecimal.ZERO,
                    (pob.getType().equals("1") || pob.getType().equals("2")) ? "2" : null); //转货权或介绍信
        } else {
            throw new RuntimeException("参数非法,禁止提交!");
        }
        return null;
    }

    @Transactional
    @Override
    public synchronized Object clear(PurchaseOrderBill record) {
        PurchaseOrderBill pob = dataDao.selectByPrimaryKey(record.getId());
        if ("0".equals(pob.getType())) { // 车号类型
            return "车号类型收货单不能清零";
        }
        if (pob.getNumberReceive().compareTo(BigDecimal.valueOf(0.0)) == 0) { //
            return "提货单没有入库!";
        }
        PurchaseOrder purchaseOrder = orderService.selectByPrimaryKey(Integer.valueOf(pob.getOrderId()));
        if (!"1".equals(purchaseOrder.getStatus())) {
            return "采购订单已关闭，收货单不能清零";
        }

        BigDecimal remain = purchaseOrder.getNumber().subtract(purchaseOrder.getNumberActualReceive());
        remain = remain.add(purchaseOrder.getNumberReceiving());
        BigDecimal unReceived = pob.getNumber().subtract(pob.getNumberReceive());// 收货单未收货部分

        if (unReceived.compareTo(remain) == 1) {
            return "操作非法!";
        }
        if (pob.getNumberSending().compareTo(BigDecimal.valueOf(0.0)) == 1 || pob.getNumberSended().compareTo(BigDecimal.valueOf(0.0)) == 1) {
            return "提货单已经用来发货，不能清零!";
        }
        PurchaseOrderBill pob1 = new PurchaseOrderBill();
        pob1.setId(record.getId());
        pob1.setNumberReceive(BigDecimal.valueOf(0.0));//
        pob1.setStatus("1");
        purchaseOrder.setNumberReceiving(pob.getNumber().add(purchaseOrder.getNumberReceiving()));
        purchaseOrder.setNumberActualReceive(purchaseOrder.getNumberActualReceive().subtract(pob.getNumberReceive()));
        orderService.updateByPrimaryKeySelective(purchaseOrder);//
        return dataDao.updateByPrimaryKeySelective(pob1);
    }

    /***
     * 订单合并收货清零
     * @param record
     * @return
     */
    @Transactional
    @Override
    public synchronized Object clearCombine(PurchaseOrderBill record) {
        PurchaseOrderBillVo pobv = new PurchaseOrderBillVo();
        pobv.setNo(record.getNo()); // clera
        List<PurchaseOrderBillVo> list = dataDao.queryByPage(pobv);
        for (PurchaseOrderBillVo pobv1 : list) {
            clearBySingle(pobv1);
        }
        return null;
    }

    public Object clearBySingle(PurchaseOrderBillVo pobv) {
        if ("0".equals(pobv.getType())) { // 车号类型
            throw new RuntimeException("车号类型收货单不能清零");
        }
        if (pobv.getNumberReceive().compareTo(BigDecimal.valueOf(0.0)) == 0) { //
            throw new RuntimeException("提货单没有入库!");
        }
        PurchaseOrder purchaseOrder = orderService.selectByPrimaryKey(Integer.valueOf(pobv.getOrderId()));
        if (!"1".equals(purchaseOrder.getStatus())) {
            throw new RuntimeException("采购订单已关闭，收货单不能清零");
        }
        BigDecimal remain = purchaseOrder.getNumber().subtract(purchaseOrder.getNumberActualReceive());
        remain = remain.add(purchaseOrder.getNumberReceiving());
        BigDecimal unReceived = pobv.getNumber().subtract(pobv.getNumberReceive());// 收货单未收货部分
        if (unReceived.compareTo(remain) == 1) {
            throw new RuntimeException("操作非法!");
        }
        if (pobv.getNumberSending().compareTo(BigDecimal.valueOf(0.0)) == 1 || pobv.getNumberSended().compareTo(BigDecimal.valueOf(0.0)) == 1) {
            throw new RuntimeException("提货单已经用来发货，不能清零!");
        }
        orderService.changeNumber(purchaseOrder.getId(),
                pobv.getNumber(),
                pobv.getNumberReceive().negate());

        purchaseService.changeNumber(
                Integer.valueOf(purchaseOrder.getPurchaseId()),
                BigDecimal.ZERO, //不影响采购数量
                pobv.getNumberReceive().negate(), //扣减已交易数量
                pobv.getNumberReceive()); // 增加交易中数量

        PurchaseOrderBill pob1 = new PurchaseOrderBill();
        pob1.setId(pobv.getId());
        pob1.setNumberReceive(BigDecimal.valueOf(0.0));//
        pob1.setStatus("1");
        return dataDao.updateByPrimaryKeySelective(pob1);
    }

    /**
     * 2017-11-25 此后废弃
     *
     * @param record
     * @param isAuthed
     * @return
     */
    @Deprecated
    @Transactional
    @Override
    public synchronized Object delete(PurchaseOrderBill record, Boolean isAuthed) {
        PurchaseOrderBill pob = dataDao.selectByPrimaryKey(record.getId());
        if (pob.getNumberReceive().compareTo(BigDecimal.valueOf(0.0)) == 1) {
            if (!isAuthed) return "收货单已填入实收数量，无权限操作";
        }
        if ((pob.getNumberSended().compareTo(BigDecimal.valueOf(0.0)) == 1) || (pob.getNumberSending().compareTo(BigDecimal.valueOf(0.0)) == 1)) {
            return "收货单已经被占用，不能删除";
        }
        PurchaseOrder purchaseOrder = orderService.selectByPrimaryKey(Integer.valueOf(pob.getOrderId()));
        BigDecimal returned = purchaseOrder.getNumber().subtract(purchaseOrder.getNumberActualReceive());
        if ("3".equals(purchaseOrder.getStatus())) { // 强制执行完毕
            Purchase purchase = new Purchase();
            Purchase purchase_ = purchaseService.selectByPrimaryKey(Integer.valueOf(purchaseOrder.getPurchaseId()));
            if (purchase_.getNumber().compareTo(returned) == -1) { //采购单数量小于强制执行完毕采购订单未提货数量
                return "采购单可售数量不足，不能退款";
            }
            purchase.setId(purchase_.getId());
            purchase.setNumberCharging(purchase_.getNumberCharging().add(returned));
            purchase.setNumber(purchase_.getNumber().subtract(returned));
            purchaseService.updateByPrimaryKeySelective(purchase);
        }
        BigDecimal remain = returned;
        remain = remain.add(purchaseOrder.getNumberReceiving());
        BigDecimal unReceived = pob.getNumber().subtract(pob.getNumberReceive());// 收货单未收货部分
        if (unReceived.compareTo(remain) == 1) {
            return "采购订单可收货数量小于收货单未提货数量，不能删除!";
        }
        PurchaseOrder po = new PurchaseOrder();
        po.setStatus("1");
        if (pob.getNumberReceive().compareTo(BigDecimal.valueOf(0.0)) == 1) {
            po.setNumberActualReceive(purchaseOrder.getNumberActualReceive().subtract(pob.getNumberReceive()));
        } else {
            po.setNumberReceiving(purchaseOrder.getNumberReceiving().subtract(pob.getNumber()));
        }
        po.setId(Integer.valueOf(pob.getOrderId()));
        orderService.updateByPrimaryKeySelective(po);//
        return dataDao.deleteByPrimaryKey(record.getId());
    }

    /***
     * @version v3.0 三期启用
     * 删除功能在未收货、未用来发货前提有效
     * 其中已收货并且未发货情况下可通过权限控制进行删除
     * @param record
     * @param isAuthed
     * @return
     */
    @Transactional
    @Override
    public synchronized Object deleteCombine(PurchaseOrderBill record, Boolean isAuthed) {
        PurchaseOrderBillVo purchaseOrderBillVo = new PurchaseOrderBillVo();
        purchaseOrderBillVo.setNo(record.getNo());
        List<PurchaseOrderBillVo> list = dataDao.queryByPage(purchaseOrderBillVo);
        for (PurchaseOrderBillVo pob : list) {
            deleteBySingle(pob, isAuthed);
        }
        return null;
    }

    /**
     * 单项删除
     *
     * @param pob
     * @return
     */
    public Object deleteBySingle(PurchaseOrderBillVo pob, boolean isAuthed) {
        if ("2".equals(pob.getCheckStatus())) { // 拒绝状态，直接删除，不需要进行数据还原
            return dataDao.deleteByPrimaryKey(pob.getId());//物理删除， 此处 @todo 应该逻辑删除
        }
        if (pob.getNumberReceive().compareTo(BigDecimal.ZERO) == 1) {
            if (!isAuthed) throw new RuntimeException("收货单已填入实收数量，无权限操作");
        }

        if ((pob.getNumberSended().compareTo(BigDecimal.ZERO) == 1) || (pob.getNumberSending().compareTo(BigDecimal.ZERO) == 1)) {
            throw new RuntimeException("收货单已经被占用，不能删除");
        }
        PurchaseOrder purchaseOrder = orderService.selectByPrimaryKey(Integer.valueOf(pob.getOrderId()));
        BigDecimal returned = purchaseOrder.getNumber().subtract(purchaseOrder.getNumberActualReceive()); // 订单未收货数量
        if ("3".equals(purchaseOrder.getStatus())) { // 强制执行完毕
            Purchase purchase_ = purchaseService.selectByPrimaryKey(Integer.valueOf(purchaseOrder.getPurchaseId()));
            if (purchase_.getNumber().compareTo(returned) == -1) { //采购单数量小于强制执行完毕采购订单未提货数量
                throw new RuntimeException("采购单采购数量小于已强制执行完毕采购订单未收货部分数量，删除");
            }
            purchaseService.changeNumber(purchase_.getId(), // 强制执行完毕的采购订单对应的采购单采购数量回吐
                    returned.negate(),
                    BigDecimal.ZERO,
                    returned);
        }
        BigDecimal remain = returned;
        remain = remain.add(purchaseOrder.getNumberReceiving());
        if (pob.getNumber().compareTo(remain) == 1) {
            throw new RuntimeException("采购订单可入库数量小于入库数量，不能删除!");
        }
        boolean isReceived = pob.getNumberReceive().compareTo(BigDecimal.ZERO) == 1; // 是否已收货
        orderService.changeNumber(Integer.valueOf(pob.getOrderId()),
                isReceived ? BigDecimal.ZERO : pob.getNumber().negate(),
                isReceived ? pob.getNumberReceive().negate() : BigDecimal.ZERO);
        if (isReceived) {
            purchaseService.changeNumber(
                    Integer.valueOf(purchaseOrder.getPurchaseId()),
                    BigDecimal.ZERO, // 交易总数不变
                    pob.getNumberReceive().negate(), // 扣减已交易
                    pob.getNumberReceive() // 增加交易中
            );
        }
        return dataDao.deleteByPrimaryKey(pob.getId());//物理删除， 此处 @todo 应该逻辑删除
    }

    @Override
    public int updateByPrimaryKey(PurchaseOrderBill record) {
        return dataDao.updateByPrimaryKey(record);
    }

    @Override
    public int queryCount(PurchaseOrderBillVo record) {
        return dataDao.queryCount(record);
    }

    @Override
    public List<PurchaseOrderBillVo> queryByPage(PurchaseOrderBillVo record) {
        return dataDao.queryByPage(record);
    }

    @Override
    public int queryCurrentDayCount() {
        return getMaxInNoOrder();
    }

    @Override
    public PurchaseOrderBill changeNumber(Integer id, BigDecimal actual, BigDecimal sended, BigDecimal sending, String status) {
        PurchaseOrderBill pob = dataDao.selectByPrimaryKey(id);
        PurchaseOrderBill purchaseOrderBill = new PurchaseOrderBill();
        purchaseOrderBill.setId(pob.getId());
        purchaseOrderBill.setNumberReceive(pob.getNumberReceive().add(actual));
        purchaseOrderBill.setNumberSended(pob.getNumberSended().add(sended));
        purchaseOrderBill.setNumberSending(pob.getNumberSending().add(sending));
        purchaseOrderBill.setStatus(status);
        if (purchaseOrderBill.getNumberSending().compareTo(BigDecimal.valueOf(0.0)) == -1
                || purchaseOrderBill.getNumberSended().compareTo(BigDecimal.valueOf(0.0)) == -1) {
            throw new RuntimeException("数据有误，入库失败");
        }
        dataDao.updateByPrimaryKeySelective(purchaseOrderBill);
        return pob;
    }


    @Override
    @Transactional
    public Object refuse(PurchaseOrderBillVo arg) {
        //
        List<PurchaseOrderBillVo> list = dataDao.queryByPage(arg);
        for (PurchaseOrderBill pob : list) {
            if (!"0".equals(pob.getCheckStatus())) {
                throw new RuntimeException("数据已经审核！");
            }
            pob.setCheckStatus("2");

            pob.setReason(arg.getReason());
            dataDao.updateByPrimaryKeySelective(pob);
            purchaseOrderService.changeNumber(
                    Integer.valueOf(pob.getOrderId()),
                    pob.getNumber().negate(),
                    BigDecimal.ZERO);
        }

        return arg.getId();
    }


}